Tutorial Study Image

C++ 文件和流


2023 年 6 月 16 日, Learn eTutorial
995

到目前为止,我们一直在使用 iostream 标准库,它提供了 cin 和 cout 方法,分别用于从标准输入读取和写入标准输出。

在本教程中,您将学习如何从文件读取和写入。这需要 fstream 库,这是一个标准 C++ 库,它引入了三种新的数据类型。这意味着我们正在使用 fstream 标准 C++ 库来从文件读取和写入。让我们看看 fstream 库定义的数据类型

 

数据类型 描述

fstream

它可用于创建新文件、向现有文件写入信息以及从现有文件读取数据。

ifstream

它用于从文件读取数据。

ofstream

此数据类型是文件流的通用表示,它同时具有 ofstream 和 ifstream 功能,允许它创建文件、向文件写入数据以及从文件读取数据。

它用于文件创建和信息写入。

 

如果您想在 C++ 中处理文件,您的 C++ 源文件中必须包含头文件 <iostream><fstream>

打开文件

在从文件读取或写入文件之前,需要先打开文件。要打开文件进行写入,您可以使用 ofstream 或 fstream 对象。此外,ifstream 对象用于仅用于读取目的打开文件。

open() 函数是 fstream、ifstream 和 ofstream 对象的组成部分,其语法如下。


void open(const char *filename, ios::openmode mode);
 

在此示例中,open() 成员函数的第二个参数提供了文件应以何种模式打开,而第一个参数则指定了要打开的文件的名称和位置。

模式标志 描述

ios::app

 

追加模式。

所有写入该文件的内容都将添加到末尾。

ios::ate

 

以读取顺序打开文件。

ios::out

以写入顺序打开文件。

ios::trunc

在打开文件之前,如果文件已存在,其内容将被截断。

 

通过使用 OR 运算符,您可以组合两个或更多这些变量。例如,如果您想以写入模式打开文件并在文件已存在时截断它,则语法如下。


ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
 

可以使用以下步骤打开文件以进行读写


fstream  afile;
afile.open("file.dat", ios::out | ios::in );
 

关闭文件

当 C++ 程序结束时,所有流的自动刷新、所有分配内存的释放以及所有打开文件的关闭都会发生。但是,对于程序员来说,在终止程序之前关闭所有打开的文件始终是一种好习惯。

close() 函数是 fstream、ifstream 和 ofstream 对象的组成部分,其具有以下功能

语法


void close();
 

写入文件

C++ 编程中的流插入运算符 (<<) 用于将信息输出到屏幕,并将数据从程序发送到文件。主要区别在于使用 ofstream 或 fstream 对象而不是 cout 对象。

从文件读取

流提取运算符 (>>) 可用于从键盘输入数据以及将数据从文件读取到程序中。cin 对象被 ifstream 或 fstream 对象替换,这是唯一的区别。

读写示例

下面提供了以读写模式打开文件的 C++ 程序。该程序将用户输入的数据写入名为 afile.dat 的文件,然后从该文件读取数据并将其输出到屏幕。

以读写模式打开文件的 C++ 程序


#include <fstream>
#include <iostream>
using namespace std;
 
int main () {
   char data[100];

   // open a file in the write mode.
   ofstream outfile;
   outfile.open("afile.dat");

   cout << "Just Writing to the file" << endl;
   cout <<"Can you please enter your name: "; 
   cin.getline(data, 100);

   // write inputted data into the file.
   outfile << data << endl;

   cout << "Please enter your age: "; 
   cin >> data;
   cin.ignore();
   
   // again write the  inputted data into the file.
   outfile << data << endl;

   // just close the opened file.
   outfile.close();

   // open a file in the read mode.
   ifstream infile; 
   infile.open("afile.dat"); 
 
   cout << "Reading from the file" << endl; 
   infile >> data; 

   // write the data on the screen.
   cout << data << endl;
   
   // again read the data from the file as well as display it.
   infile >> data; 
   cout << data << endl; 

   //just close the opened file.
   infile.close();

   return 0;
}

 

输出


Just Writing to the file
Can you please enter your name: MeghnaMariya
Please enter your age: 23
Reading from the file
MeghnaMariya
23

前面的示例使用了额外的 cin 对象函数,例如 getline() 函数用于从外部读取行,以及 ignore() 函数用于忽略上一个读取命令留下的多余字符。

文件位置指针

可以使用 istream 和 ostream 提供的成员函数移动文件位置指针。这些成员函数分别是 istream 的 seekg(“seek get”)和 ostream 的 seekp(“seek put”)。

通常,seekg 和 seekp 的参数是长整型。可以提供第二个输入来指定查找方向。查找方向可以是以下三种选项之一:ios::beg(默认)用于相对于流起始位置定位,ios::cur 用于相对于流当前位置定位,或 ios::end 用于相对于流结束位置定位。

称为文件位置指针的整数值表示文件中相对于文件起始位置的字节数。以下是一些定位“get”文件位置指针的示例


//The  position mainly to the nth byte of fileObject (assumes ios::beg)
fileObject.seekg( n );

// The position of the  n bytes forward in fileObject
fileObject.seekg( n, ios::cur );

//The  position n bytes back from the  end of fileObject
fileObject.seekg( n, ios::end );

// The position mainly at end of fileObject
fileObject.seekg( 0, ios::end );

 

让我们看一个简单的 C++ FileStream 编程示例,将内容写入文本文件 testout.txt。

C++ FileStream 示例:写入文件


#include <iostream>  
#include <fstream>  
using namespace std;  
int main () {  
  ofstream filestream("testout.txt");  
  if (filestream.is_open())  
  {  
    filestream << "Welcome to learnEtutorials.\n";  
    filestream << "The C++ Tutorial.\n";  
    filestream.close();  
  }  
  else cout <<"File opening is a fail.";  
  return 0;  
}  

 

输出


Welcome to learnEtutorials
The C++ Tutorial